﻿using In66.Net.Repository.EFCoreData;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Ord.Api.Models.EntityConfig
{
    public class OrderConfig : AbstractEntityTypeConfiguration<Order>
    {
        public override void Configure(EntityTypeBuilder<Order> builder)
        {
            base.Configure(builder);

            builder.Property(x => x.CustomerId);

            builder.Property(x => x.Amount).HasColumnType("decimal(18,4)");

            builder.Property(x => x.Remark).HasMaxLength(64);

            builder.OwnsOne(x => x.Status, y =>
            {
                y.Property(z => z.Code).HasColumnName("statuscode");
                y.Property(z => z.ChangesReason).HasColumnName("statuschangesreason").HasMaxLength(64);
            });

            builder.OwnsOne(x => x.Receiver, y =>
            {
                y.Property(z => z.Name).HasColumnName("receivername").HasMaxLength(64);
                y.Property(z => z.Phone).HasColumnName("receiverphone").HasMaxLength(64);
                y.Property(z => z.Address).HasColumnName("receiveraddress").HasMaxLength(64);
            });

            builder.HasMany(x => x.Items).WithOne().HasForeignKey(y => y.OrderId);
        }
    }
}
